package com.dcoinpay.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dcoinpay.common.utils.DataResult;
import com.dcoinpay.sys.entity.DictDetail;
import com.dcoinpay.sys.service.SysDictDetailService;

import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

/**
 * 字典明细管理
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@Api(tags = "字典明细管理")
@RestController
@RequestMapping("/sys/dictDetail")
public class DictDetailController {
	@Autowired
	private SysDictDetailService sysDictDetailService;
	
	
	@ApiOperation(value = "新增")
	@PostMapping("/add")
	@RequiresPermissions("sys:dict:add")
	public DataResult<DictDetail> add(@RequestBody DictDetail sysDictDetail) {
		if (StringUtils.isEmpty(sysDictDetail.getValue())) {
			return DataResult.fail("字典值不能为空");
		}
		QueryWrapper<DictDetail> queryWrapper = new QueryWrapper<DictDetail>();
		queryWrapper.eq("value", sysDictDetail.getValue());
		queryWrapper.eq("dict_id", sysDictDetail.getDictId());
		DictDetail q = sysDictDetailService.getOne(queryWrapper);
		if (q != null) {
			return DataResult.fail("字典名称-字典值已存在");
		}
		sysDictDetailService.save(sysDictDetail);
		return DataResult.success();
	}

	@ApiOperation(value = "删除")
	@DeleteMapping("/delete")
	@RequiresPermissions("sys:dict:delete")
	public DataResult<DictDetail> delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
		sysDictDetailService.removeByIds(ids);
		return DataResult.success();
	}

	@ApiOperation(value = "更新")
	@PutMapping("/update")
	@RequiresPermissions("sys:dict:update")
	public DataResult<DictDetail> update(@RequestBody DictDetail sysDictDetail) {
		if (StringUtils.isEmpty(sysDictDetail.getValue())) {
			return DataResult.fail("字典值不能为空");
		}
		QueryWrapper<DictDetail> queryWrapper = new QueryWrapper<DictDetail>();
		queryWrapper.eq("value", sysDictDetail.getValue());
		queryWrapper.eq("dict_id", sysDictDetail.getDictId());
		DictDetail q = sysDictDetailService.getOne(queryWrapper);
		if (q != null && !q.getId().equals(sysDictDetail.getId())) {
			return DataResult.fail("字典名称-字典值已存在");
		}

		sysDictDetailService.updateById(sysDictDetail);
		return DataResult.success();
	}

	@ApiOperation(value = "查询列表数据")
	@PostMapping("/list")
	@RequiresPermissions("sys:dict:list")
	public DataResult<Object> findListByPage(@RequestBody DictDetail sysDictDetail) {
		Page<DictDetail> page = new Page<DictDetail>(sysDictDetail.getPage(), sysDictDetail.getLimit());
		if (sysDictDetail == null || StringUtils.isEmpty(sysDictDetail.getDictId())) {
			return DataResult.success();
		}
		IPage<DictDetail> iPage = sysDictDetailService.listByPage(page, sysDictDetail.getDictId());
		return DataResult.success(iPage);
	}
	
	
    @ApiOperation(value = "根据key获取数据字典数据")
    @GetMapping("/findByKeys")
    public DataResult<Object> findByKeys(String keys) {
        if (StringUtils.isEmpty(keys)) {
            return DataResult.fail("字典值不能为空");
        }
        return DataResult.success(sysDictDetailService.getByKeyMap(keys.split(",")));
    }

}
